﻿using System;
using System.IO;
using System.Reflection;

namespace HAOest.Environment
{
    /// <summary>
    /// 获取信息类
    /// <para>作者：邓斌HADB</para>
    /// <para>修订：2012-09-29 V1.1.19.0 添加了AssemblyVersion</para>
    /// </summary>
    public class Info
    {
        /// <summary>
        /// 获取程序集版本
        /// </summary>
        public static string AssemblyVersion
        {
            get
            {
                return Assembly.GetCallingAssembly().GetName().Version.ToString();
            }
        }

        /// <summary>
        /// 获取指定文件的程序集版本
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetAssemblyVersion(string path = null)
        {
            if (path == null)
            {
                return AssemblyVersion;
            }
            else
            {
                return Assembly.LoadFile(path).GetName().Version.ToString();
            }
        }

        /// <summary>
        /// 获取程序集Build号
        /// </summary>
        public static int BuildNumber
        {
            get
            {
                return Assembly.GetCallingAssembly().GetName().Version.Build;
            }
        }

        /// <summary>
        /// 获取指定文件的程序集版本
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static int GetBuildNumber(string path = null)
        {
            if (path == null)
            {
                return BuildNumber;
            }
            else
            {
                return Assembly.LoadFile(path).GetName().Version.Build;
            }
        }

        /// <summary>
        /// 获取当前执行的文件所在的目录（末尾不含"\"）
        /// </summary>
        /// <returns></returns>
        public static string GetDirectory()
        {
            DirectoryInfo di = new DirectoryInfo(Assembly.GetCallingAssembly().Location);
            return di.Parent.FullName;
        }

        /// <summary>
        /// 获取系统版本名
        /// </summary>
        public static string OSVersionString
        {
            get
            {
                return System.Environment.OSVersion.VersionString;
            }
        }

        /// <summary>
        /// 获取操作系统版本
        /// </summary>
        public static OS OSVersion
        {
            get
            {
                Version version = System.Environment.OSVersion.Version;
                if ((version.Major == 6) && (version.Minor == 1))
                    return OS.Win7;
                else if ((version.Major == 5) && (version.Minor == 1))
                    return OS.WinXP;
                else if ((version.Major == 6) && (version.Minor == 2))
                    return OS.Win8;
                else return OS.Unknow;
            }
        }

        /// <summary>
        /// 操作系统类型
        /// </summary>
        public enum OS
        {
            /// <summary>
            /// WindowsXP
            /// </summary>
            WinXP,
            /// <summary>
            /// Windows7
            /// </summary>
            Win7,
            /// <summary>
            /// Windows8
            /// </summary>
            Win8,
            /// <summary>
            /// 未知（太老或太新）
            /// </summary>
            Unknow
        }
    }
}
